package com.zzjson.springbootjdbc.webflux;

import com.zzjson.springbootjdbc.domain.User;
import com.zzjson.springbootjdbc.repository.UserRepository;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2019 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : com.zzjson.springbootjdbc.webflux</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2019年11月04日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
@Component
public class UserHandler {
	private final UserRepository userRepository;

	public UserHandler(UserRepository userRepository) {
		this.userRepository = userRepository;
	}

	public Mono<ServerResponse> save(ServerRequest serverRequest) {
		//	在 Spring Web MVC 中使用@RequestBody
		//	在 Spring Web Flux 中使用ServerRequest
		//	Mono<User> 类似于Optional<User>
		Mono<User> userMono = serverRequest.bodyToMono(User.class);
		//map 相当于转化工作
		Mono<Boolean> booleanMono = userMono.map(userRepository::save);
		Mono<Boolean> save = userMono.map(userRepository::save);
		return ServerResponse.ok().body(booleanMono, Boolean.class);
	}

}